package io.renren.modules.app.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import io.renren.modules.app.entity.AlipayNotifyEntity;
import io.renren.modules.app.service.AlipayNotifyService;
import io.renren.modules.app.utils.OrderUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @Auther zxl
 * @Date 2018/12/19 15:57
 * @Description AliPay参数
 **/
@ConfigurationProperties(prefix = "alipay")
@Component
public class AlipayConfigs {

    private Logger logger = LoggerFactory.getLogger(getClass());

    //APPID
    private  String app_id = "2016091900548619";

    private  String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCV6KonBoC1nc9TswnnmrkkH5Pf3kMGxF2+wLMXRbcssPQQjJuKxohY9nlmPMvL6tx7L9scGqB0sMahtk3AUt+C2gKSZsYI5JcVolmlxGcxtnysRbYSqiGJV0nfd4bRJKNODlnCVi5H1ed6J8Wz7Wy+r4lXMW8Q9UiMWgP5V4vF3Rtcpa4iPTJRoAylgVYe1oMPvgOGJZ5etl+Qf8V3rrs0xics4mBOWZvH9mUqyWxUkT7nfLQQYVyfBgnt8JIfgOeByurD3BiECtGse9/hhG/1CLzOMDzdWhkPO9jlvststyPKRJwBsF9JCyLzyjj0m+mERwgGLDXcR+F2GbHCh2onAgMBAAECggEAde0wpUCNUdHR/hLy8LobeNpKdqqe6Zzh7iQi/mS+Yy7U4Ivr9DCXo/xIpe+flkuspE+SOzqLDSDfMOl7opbO813S9h5ZHTfNzpwHj8rALzBSRScVwvsJJMSA/YVGjhIXUVkirSkkgSg5TzQoZ5GXoS3viaL+r0V1i8toIucGsmA3bz8eyg+OTepe2UXqn9YPeZwgOxuU8LP8KRJfmootKNsXO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Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB";

//    129.204.110.142
    private  String notify_url = "http://129.204.110.142:8080/check-car/app/alipay/alipayNotifyNotice";


    private  String return_url = "http://129.204.110.142:8080/check-car/app/alipay/alipayReturnNotice";

    private  String sign_type = "RSA";

    public  String charset = "utf-8";

    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String format;

    private String log_path;

    private String product_code;

    @Autowired
    AlipayNotifyService alipayNotifyService;

    public String refund(AlipayNotifyEntity entity) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(this.getGatewayUrl(), this.getApp_id(),
                this.getMerchant_private_key(), "json", this.getCharset(),
                this.getAlipay_public_key(), this.getSign_type());
        //创建API对应的request类
        AlipayTradeRefundRequest tradeRefundRequest = new AlipayTradeRefundRequest();
        String out_request_no = OrderUtils.generate_out_request_no(entity.getId());
        tradeRefundRequest.setBizContent("{" +
                " \"out_trade_no\":\""+entity.getOrderId()+"\"," +
                " \"trade_no\":\""+entity.getTradeNo()+"\"," +
                " \"out_request_no\":\""+out_request_no+"\"," +
                " \"refund_amount\":\""+entity.getBuyerPayAmount()+"\"" +
                " }");//设置业务参数
        //通过alipayClient调用API，获得对应的response类
        AlipayTradeRefundResponse response = alipayClient.execute(tradeRefundRequest);
        System.out.print(response.getBody());

        AlipayNotifyEntity alipayNotifyEntity = new AlipayNotifyEntity();
        alipayNotifyEntity.setOrderId(entity.getOrderId());
        alipayNotifyEntity.setOutRequestNo(out_request_no);
        boolean b = alipayNotifyService.update(alipayNotifyEntity);
        logger.info(entity.getOrderId()+"，退款更新out_trade_no："+out_request_no+" / "+b);

        return response.getBody();
    }


    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getLog_path() {
        return log_path;
    }

    public void setLog_path(String log_path) {
        this.log_path = log_path;
    }

    public String getProduct_code() {
        return product_code;
    }

    public void setProduct_code(String product_code) {
        this.product_code = product_code;
    }

    public String getApp_id() {
        return app_id;
    }

    public void setApp_id(String app_id) {
        this.app_id = app_id;
    }

    public String getMerchant_private_key() {
        return merchant_private_key;
    }

    public void setMerchant_private_key(String merchant_private_key) {
        this.merchant_private_key = merchant_private_key;
    }

    public String getAlipay_public_key() {
        return alipay_public_key;
    }

    public void setAlipay_public_key(String alipay_public_key) {
        this.alipay_public_key = alipay_public_key;
    }

    public String getNotify_url() {
        return notify_url;
    }

    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }

    public String getReturn_url() {
        return return_url;
    }

    public void setReturn_url(String return_url) {
        this.return_url = return_url;
    }

    public String getSign_type() {
        return sign_type;
    }

    public void setSign_type(String sign_type) {
        this.sign_type = sign_type;
    }

    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getGatewayUrl() {
        return gatewayUrl;
    }

    public void setGatewayUrl(String gatewayUrl) {
        this.gatewayUrl = gatewayUrl;
    }
}
